package springboot.Tools.Timer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import springboot.Tools.HttpApi;
import springboot.controller.vueTest;
import springboot.doMain.WxGroup;
import springboot.doMain.WxGroupChatAdmin;
import springboot.doMain.WxUser;
import springboot.http.WXWorkApi;
import springboot.service.impl.OverTimeDataServiceImpl;
import springboot.service.impl.WXGroupChatAdminServiceImpl;
import springboot.service.impl.WXGroupServiceImpl;
import springboot.service.impl.WXUserServiceImpl;

import java.util.ArrayList;
import java.util.List;

//定时任务
@Component
@EnableScheduling
public class TimerSocketMessage {
	
	
	final
	WXGroupChatAdminServiceImpl wxGroupChatAdminService;
	
	final
	WXUserServiceImpl wxUserService;
	
	
	final
	WXGroupServiceImpl wxGroupService;
	
	final
	OverTimeDataServiceImpl overTimeDataService;
	
	public TimerSocketMessage(OverTimeDataServiceImpl overTimeDataService, WXUserServiceImpl wxUserService, WXGroupServiceImpl wxGroupService, WXGroupChatAdminServiceImpl wxGroupChatAdminService) {
		this.overTimeDataService = overTimeDataService;
		this.wxGroupService = wxGroupService;
		this.wxUserService = wxUserService;
		this.wxGroupChatAdminService = wxGroupChatAdminService;
	}
	
	/**
	 * 每天十点统计工作数据，入库
	 */
	@Scheduled(cron = "0 0 10 * * ?")
	public void insertMsgAndProData() {
		overTimeDataService.insertOverTimeData();
	}
	
	
	@Scheduled(cron = "20 30 17 * * ?")
	public void insertUser() {
		String departmentInfo = WXWorkApi.getDepartmentInfo();
		JSONArray jsonArray = JSONArray.parseArray(departmentInfo);
		jsonArray.forEach(o -> wxUserService.insert(new WxUser(JSON.parseObject(o + ""))));
	}
	
//	每天六点向数据库插入销售数据
	@Scheduled(cron = "0 0 6 * * ?")
	public void insert() {
		insertSellerData();
	}
	void insertSellerData() {
		insertSellerData(wxUserService, wxGroupChatAdminService);
	}
	
	public static void insertSellerData(WXUserServiceImpl wxUserService, WXGroupChatAdminServiceImpl wxGroupChatAdminService) {
		List<WxUser> wxUsers = wxUserService.selAllRemark();
		if (wxUsers == null) {
			return;
		}
		for (int i = 10; i >= 0; i--) {
			int finalI = i;
			List<WxGroupChatAdmin> wxGroupChatAdmins = new ArrayList<>();
			wxUsers.forEach(wxUser -> {
				/*确保查到的销售名单有效*/
				if (wxUser == null) {
					return;
				}
				String userId = wxUser.getUserId();
				String sellerDataByDay = WXWorkApi.getSellerDataByDay(finalI + 1, userId);
				/*如果获取到的数据为空，则跳出*/
				if (sellerDataByDay == null) {
					return;
				}
				WxGroupChatAdmin wxGroupChatAdmin = new WxGroupChatAdmin(sellerDataByDay);
				wxGroupChatAdmin.setOwner(userId);
				wxGroupChatAdmins.add(wxGroupChatAdmin);
			});
			/*数组不为空才，插入数据*/
			if (!wxGroupChatAdmins.isEmpty()) {
				wxGroupChatAdminService.insertDayGroupChat(wxGroupChatAdmins);
			}
		}
	}
	
	@Scheduled(cron = "2 30 1 * * ?")
	public void insertGroup() {
		String groupList = WXWorkApi.getGroupList();
		if (groupList == null) {
			return;
		}
		JSONObject jsonObject = JSON.parseObject(groupList);
		JSONArray group_chat_list = jsonObject.getJSONArray("group_chat_list");
		String token = WXWorkApi.getToken();
		
		for (Object o : group_chat_list) {
			String chat_id = WXWorkApi.getGroupInfo(
					JSONObject.parseObject(o + "").getString("chat_id"), token);
			JSONObject groupInfo = JSON.parseObject(chat_id);
			wxGroupService.insert(new WxGroup(groupInfo));
		}
		
	}
}
